
# -*- coding: utf-8 -*-
#
# uruk-apt
#  
#  Copyright 2023 alimiracle <alimiracle@riseup.net>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
import apt
import apt_pkg
import os

def update_package_information():
    upgrade_packages = []
    try:
        cache = apt.Cache()
        cache.update()
        cache.open()

        for pkg in cache:
            if cache[pkg.name].is_upgradable:
                upgrade_packages.append(pkg.name)

    except Exception as e:
        print(f"Error updating package information: {e}")
    return upgrade_packages


def upgrade_packages():
    try:
        cache = apt.Cache()
        cache.update()
        cache.open()
        # Set deferred configuration to True to make the upgrade non-interactive
        cache.set_deferred_config(True)
        # Mark all upgradable packages for upgrade
        for pkg in cache:
            if cache[pkg.name].is_upgradable:
                pkg.mark_upgrade()

        # Perform the actual upgrade
        cache.upgrade()

        # Commit the changes
        cache.commit()

        print("Packages successfully upgraded.")
    except Exception as e:
        print(f"Error upgrading packages: {e}")
        return -1
    return 0
def upgrade_package(package_name):
    try:
        cache = apt.Cache()
        cache.update()
        cache.open()

        # Check if the specified package is upgradable
        if cache[package_name].is_upgradable:
            # Mark the package for upgrade
            cache[package_name].mark_upgrade()
        # Set deferred configuration to True to make the upgrade non-interactive
            cache.set_deferred_config(True)

            # Perform the actual upgrade
            cache.upgrade()

            # Commit the changes
            cache.commit()

            print(f"Package {package_name} successfully upgraded.")
        else:
            print(f"Package {package_name} is not upgradable.")
    except Exception as e:
        print(f"Error upgrading package {package_name}: {e}")
        return -1
    return 0
def calculate_upgradable_packages():
    try:
        cache = apt.Cache()
        cache.open()
        upgradable_packages = [pkg for pkg in cache if cache[pkg.name].is_upgradable]
        return len(upgradable_packages)
    except Exception as e:
        print(f"Error calculating upgradable packages: {e}")
        return -1
